/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
 * Description:
 * Author: g00421808
 * Create: 6/24/2022
 * Notes:
 */

#ifndef JSBIND_FUNCTION_DEFINER_H
#define JSBIND_FUNCTION_DEFINER_H

#include "jsbind/binding.h"
#include "jsbind/config.h"

#if JSBIND_USING_NAPI
# include "jsbind/binder/napi/napi_function_binder.h"
# include "jsbind/binder/napi/napi_pfunction_binder.h"
#endif
namespace Jsb {
class FunctionDefiner {
public:
    template <typename R, typename... Args>
    FunctionDefiner(
            const char* name,
            R(*func)(Args...)
    ) {
#if JSBIND_USING_NAPI
        typedef NapiFunctionBinder<
                decltype(func), 
                R,
                Args...
        > Binder;
#endif

        Binding::RegisterFunction(name,
                                  Binder::AddInvoker(func),
                                  &Binder::GetInstance());
    }

    ~FunctionDefiner() = default;
};

class PFunctionDefiner {
public:
    template <typename R, typename... Args>
    PFunctionDefiner(
        const char* name,
        R(*func)(Args...)
    ) {
#if JSBIND_USING_NAPI
        typedef NapiPFunctionBinder<
            decltype(func), 
            R,
            Args...
        > Binder;
#endif

        Binding::RegisterFunction(name,
                                  Binder::AddInvoker(func),
                                  &Binder::GetInstance());
    }

    ~PFunctionDefiner() = default;
};
} // namespace Jsb

#endif //JSBIND_FUNCTION_DEFINER_H
